package com.lcc.realtime.app.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.lcc.realtime.app.BaseAppV1;
import com.lcc.realtime.util.FlinkSinkUtil;
import com.lcc.realtime.util.LccUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import static com.lcc.realtime.common.Constant.*;

public class DwdLogApp extends BaseAppV1 {
    private final String DISPLAY = "display";
    private final String START = "start";
    private final String PAGE = "page";

    public static void main(String[] args) {
        new DwdLogApp().init("Dwd", 10001, 1, "Dwd_log", "DwdLogApp", "ods_log");
    }

    public void handle(StreamExecutionEnvironment env,
                       DataStreamSource<String> stream) {
//        stream.print();
        //判断新老用户行为
        SingleOutputStreamOperator<JSONObject> validateStream = validateNewOrOld(stream);
        //分流
        HashMap<String, DataStream<JSONObject>> splitStream = splitStream(validateStream);
        //输入kafka
        writeTOKafka(splitStream);
//        splitStream.get(START).print(START);
//        splitStream.get(DISPLAY).print(DISPLAY);
//        splitStream.get(PAGE).print(PAGE);
    }

    private void writeTOKafka(HashMap<String, DataStream<JSONObject>> splitStream) {
        splitStream.get(START)
                .map(JSONAware::toJSONString)
                .addSink(FlinkSinkUtil.getKafkaSink(TOPIC_DWD_START));
        splitStream.get(DISPLAY)
                .map(JSONAware::toJSONString)
                .addSink(FlinkSinkUtil.getKafkaSink(TOPIC_DWD_DISPLAY));
        splitStream.get(PAGE)
                .map(JSONAware::toJSONString)
                .addSink(FlinkSinkUtil.getKafkaSink(TOPIC_DWD_PAGE));

    }

    private HashMap<String, DataStream<JSONObject>> splitStream(SingleOutputStreamOperator<JSONObject> stream) {
        OutputTag<JSONObject> display = new OutputTag<JSONObject>(DISPLAY) {};//
        OutputTag<JSONObject> page = new OutputTag<JSONObject>(PAGE) {};//
        SingleOutputStreamOperator<JSONObject> resultStream = stream.process(new ProcessFunction<JSONObject, JSONObject>() {
            @Override
            public void processElement(JSONObject in,
                                       Context ctx,
                                       Collector<JSONObject> out) throws Exception {
                if (in.containsKey("start"))
                    out.collect(in);
                else {
                    if (in.containsKey("display")) {
                        JSONArray displays = in.getJSONArray(DISPLAY);
                        for (int i = 0; i < displays.size(); i++) {
                            JSONObject o = displays.getJSONObject(i);
                            o.putAll(in.getJSONObject("common"));
                            o.putAll(in.getJSONObject(PAGE));
                            o.put("ts", in.getLong("ts"));
                            ctx.output(display, o);
                        }
                    }
                    if (in.containsKey("page")) {
                        ctx.output(page, in);
                    }
                }
            }

        });
        DataStream<JSONObject> pageStream = resultStream.getSideOutput(page);
        DataStream<JSONObject> displayStream = resultStream.getSideOutput(display);
        HashMap<String, DataStream<JSONObject>> result = new HashMap<>();
        result.put(START, resultStream);
        result.put(PAGE, pageStream);
        result.put(DISPLAY, displayStream);
        return result;
    }

    private SingleOutputStreamOperator<JSONObject> validateNewOrOld(DataStreamSource<String> stream) {
        return
                stream.map(JSON::parseObject)
                        .assignTimestampsAndWatermarks(
                                WatermarkStrategy
                                        .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                        .withTimestampAssigner((x, ts) -> x.getLong("ts"))
                        )
                        .keyBy(x -> x.getJSONObject("common").getString("mid"))
                        .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                        .process(new ProcessWindowFunction<JSONObject, JSONObject, String, TimeWindow>() {
                            ValueState<Long> first_win;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                first_win = getRuntimeContext().getState(new ValueStateDescriptor<Long>(
                                        "first_win",
                                        Long.class));
                            }

                            @Override
                            public void process(String k,
                                                Context ctx,
                                                Iterable<JSONObject> in,
                                                Collector<JSONObject> out) throws Exception {
                                if (first_win.value() == null) {
                                    List<JSONObject> jsonObjects = LccUtil.toList(in);
                                    JSONObject min = Collections.min(jsonObjects, Comparator.comparing(o -> o.getLong("ts"))

//                                    (x,y)->x.getLong("ts").compareTo(y.getLong("ts"))

//                                    new Comparator<JSONObject>() {
//                                @Override
//                                public int compare(JSONObject o1, JSONObject o2) {
//                                    return o1.getLong("ts").compareTo(o2.getLong("ts"));
//                                }
//                            }
                                    );
                                    for (JSONObject object : in) {
                                        if (object.equals(min)) {
                                            object.getJSONObject("common").put("is_new", 1);
                                        } else
                                            object.getJSONObject("common").put("is_new", 0);
                                        out.collect(object);
                                    }
                                } else {
                                    for (JSONObject object : in) {
                                        object.getJSONObject("common").put("is_new", 0);
                                        out.collect(object);
                                    }
                                }
                            }
                        });
    }


}
